	function autocomplete(div,url,campo){
		$(div).autocomplete({
			source: function( request, response ) {
				$.ajax({
					url:baseUrl(url),
					dataType: "json",
					type:'post',
					style: "full",
					maxRows: 12,
					data: {
						'campo': campo,
						'entrada':$(div).val()
						},

					success: function( data ) {
							resposta=[];
							response($.map(data , function(item){
								qtd = item.length;

								for(var indice = 0; indice < qtd ; indice++){
									resposta.push({
										label:item[indice],
										value:item[indice]
									});
								}
							return resposta;
							}));


					},
					beforeSend: function() {},
					complete: function(){}
				});
			},
			minLength: 1,
			select: function( event, ui ) {},
			open: function() {
				$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
			},
			close: function() {
				$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
			}
		});
		}

	function validaCPF(input){
			valor = input.val();
			if(valor.match(/\d{3}\.\d{3}\.\d{3}\-\d{2}/g)==null){
				return false;
			}
			return true;
	}

	function validaEmail(input){
		email = input.val();
		tamanho = email.length;
		if(tamanho<=10 ){
			return false;
		}else{
		arroba = email.lastIndexOf('@');
		ponto = email.lastIndexOf('.');
			if(arroba ==-1 || ponto == -1 || ponto == tamanho){
			return false;
		}else{
			if(arroba<=4){
				return false;
			}
		}
		}
		return true;
	}


	function baseUrl(url){
		path  = window.location.pathname;
		arr = window.location.pathname.split("/", 3);
		return window.location.protocol+'//'+window.location.host+'/'+arr[1]+'/'+arr[2]+'/'+url;
	}

	function dialogMensagem(titulo , mensagem){
		$("#contentModal").html('');
		$("#contentModal").dialog({
			title:titulo,
			modal:true,
			height:100,
			width:'30%',
			draggable:false,
			zindex:1000
		});
		$("#contentModal").html(mensagem);
	}


	function loader(){
		$("<div><p>Carregando...<br><img src='"+baseUrl('adminsource/img/loader.gif')+"' /></p></div>")
			.attr("id", "loader")
			.appendTo("body")
			.dialog({
				title:"Processando Requisição...",
				modal:true,
				height:100,
				width:250,
				draggable:false,
				zindex:2000
			});
	}



	function actionAjax(data){
		switch (data.action) {

		case 'loadElementOptions':
			element = $('#'+data.element);
			element.text('');
			element.append(data.options);
			break;

		case 'redirect':
			$(window).attr('location',data.redirect.url);
			break;
		case 'message':
			dialogMensagem('Importante!'  , data.message.text);
			break;
		case 'refreshMessage':
			$("#contentModal").html('');
			$( "#contentModal" ).dialog({
				title:'Informação do Sistema.',
				modal:true,
				height:150,
				width:'30%',
				draggable:false,
				zindex:2000,
				buttons: {
					Continuar :  function() {
						$( this ).dialog( "close" );
						document.location = document.location;
					}
				}
			});

			$("#contentModal").html(data.refreshMessage.message);
			break;
		}
	}


	function mvcAjax(url ,dados){
		$.ajax({
			url:baseUrl(url),
			data:dados,
			dataType:'json',
			success:function(data){
				actionAjax(data);
			},
			error:function(e){
				dialogMensagem('Importante!'  ,'Não foi possível realizar a Requisição.');
				return false;
				}
			});
	}

	function actionMore(objeto){
		html="<section class='mais'>"+$(objeto).parent('section').html();



		teste = html.split('(-)');


		if(teste.length <=1){
			html+="<input type='button'  onclick='actionRemoveMore(this);' value='(-) Remover'></section>";
		}

		$(objeto).parent('section').after(html);
		readyMaskSet();
		$('input[type="text"]').each(function(){
			$(this).setMask();
		});
	}

	function actionRemoveMore(obj){
		$(obj).parent('section').remove();
	}


	function readyMaskSet(){
		$('input[type="text"]').each(function(){

			alt = $(this).attr('alt');
			switch (alt) {
			case 'date':
				$(this).datepicker({
					 changeMonth: true,
			            changeYear: true
				});
				break;
			}

			mask = $(this).attr('mask');
			switch (mask) {
			case 'email':
				$(this).focusout(function(){
					validacao = validaEmail( $(this) );
					if(!validacao){
						 dialogMensagem('Inform do Sistema' , 'E-mail informado é inválido');
						 $(this).val('');
					}
				});
				break;
			}
		});
	}





